package com.chif.gulimall.product.web;

import com.chif.gulimall.product.entity.CategoryEntity;
import com.chif.gulimall.product.service.CategoryService;
import com.chif.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author liuzilong
 * @date 2022/11/21 16:36
 */
@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redisson;

    @GetMapping({"/","/index.html"})
    public String indexPage(Model model){
        List<CategoryEntity> categoryEntities=categoryService.getLevel1Categorys();
        model.addAttribute("categories",categoryEntities);
        return "index";
    }



    @ResponseBody
    @GetMapping("/index/json/catalog.json")
    public Map<String,List<Catelog2Vo>> getCatelogJson(){
        Map<String,List<Catelog2Vo>> catelogJson=categoryService.getCatalogJsonWithCacheable();
        return catelogJson;
    }

    @GetMapping("/test")
    public String redisson(){
        //获取一把锁，锁名字一样就是同一把锁
        RLock lock = redisson.getLock("my-lock");

        //加锁
        lock.lock();//阻塞式等待
        try {
            System.out.println("加锁成功，执行成功");
        }finally {
            //解锁
            lock.unlock();
        }
        return "hello";
    }
}
